package org.aphelie.torg.ruler.internal.core.character;

import java.io.PrintWriter;

import org.aphelie.torg.ruler.core.character.ICharacter;
import org.aphelie.torg.ruler.core.character.ICharacterExtension;
import org.eclipse.core.runtime.CoreException;
import org.w3c.dom.Node;


public class Character extends CharacterBase implements ICharacter {
    private static final long serialVersionUID = 1L;
    private String fClassname;
    private boolean fHasExtensibleAPI;

    public Character(boolean readOnly) {
        super(readOnly);
    }

    public String getClassName() {
        return fClassname;
    }

    public ICharacter getCharacter() {
        return this;
    }

    /* TODO
    void load(BundleDescription bundleDescription, PDEState state) {
        fClassname = state.getClassName(bundleDescription.getBundleId());
        fHasExtensibleAPI = state.hasExtensibleAPI(bundleDescription.getBundleId());
        super.load(bundleDescription, state);
    }
    */

    void load(Node node, String schemaVersion) {
        fClassname = getNodeAttribute(node, "class"); //$NON-NLS-1$
        super.load(node, schemaVersion);
    }

    public void reset() {
        fClassname = null;
        super.reset();
    }

    public void setClassName(String newClassName) throws CoreException {
        ensureModelEditable();
        String oldValue = fClassname;
        fClassname = newClassName;
        firePropertyChanged(P_CLASS_NAME, oldValue, fClassname);
    }

    public void restoreProperty(String name, Object oldValue, Object newValue) throws CoreException {
        if (name.equals(P_CLASS_NAME)) {
            setClassName(newValue != null ? newValue.toString() : null);
            return;
        }
        super.restoreProperty(name, oldValue, newValue);
    }

    public void write(String indent, PrintWriter writer) {
        writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
        if (getSchemaVersion() != null) {
            writer.println("<?eclipse version=\"" + getSchemaVersion() + "\"?>"); //$NON-NLS-1$ //$NON-NLS-2$
        }
        writer.print("<plugin"); //$NON-NLS-1$
        if (getId() != null) {
            writer.println();
            writer.print("   id=\"" + getId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
        }
        if (getName() != null) {
            writer.println();
            writer.print("   name=\"" + getWritableString(getName()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
        }
        if (getVersion() != null) {
            writer.println();
            writer.print("   version=\"" + getVersion() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
        }
        if (getProviderName() != null) {
            writer.println();
            writer.print("   provider-name=\"" //$NON-NLS-1$
                    + getWritableString(getProviderName()) + "\""); //$NON-NLS-1$
        }
        if (getClassName() != null) {
            writer.println();
            writer.print("   class=\"" + getClassName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
        }
        writer.println(">"); //$NON-NLS-1$
        writer.println();

        String firstIndent = "   "; //$NON-NLS-1$

        if (getSketch() != null) {
            writeChild(firstIndent, getSketch(), writer);
        }
        
        
        Object[] children = null;
        // add runtime
        
        children = getLibraries();
        if (children.length > 0) {
            writeChildren(firstIndent, "runtime", children, writer); //$NON-NLS-1$
            writer.println();
        }
 
        // add requires
        /* TODO
        children = getImports();
        if (children.length > 0) {
            writeChildren(firstIndent, "requires", children, writer); //$NON-NLS-1$
            writer.println();
        }
        */

        /* TODO
        children = getExtensionPoints();
        for (int i = 0; i < children.length; i++) {
            ((ICharacterExtensionPoint) children[i]).write(firstIndent, writer);
        }

        if (children.length > 0)
            writer.println();
        */

        // add extensions
        children = getExtensions();
        for (int i = 0; i < children.length; i++) {
            ((ICharacterExtension) children[i]).write(firstIndent, writer);
        }
        if (children.length > 0)
            writer.println();

        writer.println("</plugin>"); //$NON-NLS-1$
    }

    public boolean hasExtensibleAPI() {
        return fHasExtensibleAPI;
    }

}
